package com.qf.common.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author: sin
 * @Date: 2024/10/10 16:03
 * @Description: Json数据处理工具
 **/
@Slf4j
public class JsonUtil {

    private static final ObjectMapper INSTANCE = new ObjectMapper();

    public static String toJsonString(Object obj) {
        try {
            return INSTANCE.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            // todo Jackson 出参打印包含 Java 8 新日期出错问题
            return obj.toString();
        }
    }

    public static <T> T parseObject(String json, Class<T> clazz) {
        try {
            return INSTANCE.readValue(json, clazz);
        } catch (JsonProcessingException e) {
            log.error("JsonUtil parseObject error: {}", e.getMessage());
            return null;
        }
    }

}